<template>
  <div v-if="visible">
    <el-dialog :visible.sync="visible"
               title="配置同步指纹设备"
               :modal="false"
               class="user-dialog add-or-updata"
               width="60%"
               :before-close="closeDialog"
               :close-on-click-modal="false"
               :close-on-press-escape="false">
      <template>
        <el-button type="primary"
                   plain
                   icon="el-icon-plus"
                   style="margin-bottom: 20px;margin-top: -20px;"
                   @click="openDevice()">添加设备</el-button>
      </template>
      <table-template :tableData='tableData'
                      :loading='dataListLoading'
                      :fieldManagement='false'
                      :selectionChange='selectionChange'
                      height="300px"
                      :dataList='dataList'>
        <template slot="name-after">
          <el-table-column prop="deviceType"
                           align="center"
                           label="设备类型"
                           show-overflow-tooltip
                           min-width="80">
            <template slot-scope="scope">
              <span v-if="scope.row.deviceType === 1">热水表</span>
              <span v-if="scope.row.deviceType === 2">冷水表</span>
              <span v-if="scope.row.deviceType === 3">电表</span>
              <span v-if="scope.row.deviceType === 4">门锁</span>
              <span v-if="scope.row.deviceType === 5">门禁</span>
              <span v-if="scope.row.deviceType === 6">网关</span>
              <span v-if="scope.row.deviceType === 7">门禁考勤一体机</span>
              <span v-if="scope.row.deviceType === 8">摄像头</span>
              <span v-if="scope.row.deviceType === 9">音频</span>
              <span v-if="scope.row.deviceType === 10">信息屏</span>
            </template>
          </el-table-column>
        </template>
        <el-table-column :label="$t('handle')"
                         fixed="right"
                         header-align="center"
                         align="center"
                         width="100">
          <template slot-scope="scope">
            <el-button type='text'
                       class='brand'
                       v-if="$hasPermission('tb:device:updateDevice')"
                       @click='Remove(scope.row)'
                       size='small'>移除</el-button>
          </template>
        </el-table-column>
      </table-template>
      <el-pagination :current-page="page"
                     :page-sizes="[10, 20, 50, 100]"
                     :total="total"
                     layout="total, sizes, prev, pager, next"
                     @size-change="pageSizeChangeHandle"
                     @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <div style="height: 100px;"></div>
      <!-- <template slot="footer">
        <el-button @click="closeDialog()">{{ $t('cancel')}}</el-button>
        <el-button type="primary"
                   :loading="dataListLoading"
                   @click="dataFormSubmitHandle()">{{ $t('confirm')}}</el-button>
      </template> -->
    </el-dialog>
    <!-- 添加设备 -->
    <addDevice ref="addDevice"
               @callBack="getDataList" />
  </div>
</template>

<script>
import tableTemplate from '@/components/table-template'
import mixinViewModule from '@/mixins/view-module'
import minFormValidate from '@/mixins/form-validate'
import debounce from 'lodash/debounce'
import addDevice from './add-device'
export default {
  mixins: [mixinViewModule, minFormValidate],
  props: {
    terminalId: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      mixinViewModuleOptions: {
        activatedIsNeed: false,
        getDataListURL: '/tb/device/queryFinDcList?fingerprintDeviceConfigure=0', // 数据列表接口，API地址
        getDataListIsPage: true, // 数据列表接口，是否需要分页？
        addOrUpdateURL: '', // 添加或修改接口，API地址
        getDataInfoURL: '', // 数据详情，API地址
        deleteURL: '', // 删除接口，API地址
        deleteIsBatch: true, // 删除接口，是否需要批量？
        exportURL: '' // 导出接口，API地址
      },
      selectItems: [],
      visible: false,
      dataForm: {},
      dataList: []
    }
  },
  components: {
    tableTemplate,
    addDevice
  },
  computed: {
    tableData () {
      return [
        { label: '设备id', width: '120', prop: 'deviceId' },
        { label: '设备名称', width: '100', prop: 'name' },
        { label: '所在位置', width: '100', prop: 'areaName' }
      ]
    }
  },
  methods: {
    closeDialog () {
      this.visible = false
      this.$emit('callBack')
    },
    selectionChange (data) {
      this.selectItems.splice(0, this.selectItems.length, ...data)
    },
    openDevice () {
      this.$refs.addDevice.init()
    },
    init (data) {
      this.getDataList()
      //   let url = this.mixinViewModuleOptions.getDataListURL
      //   this.$http.get(url).then(({ data: res }) => {
      //     if (res.code !== 0) {
      //       return this.$message.error(res.msg)
      //     }
      //     this.dataList = res.data
      //   })
      //   this.getData({ url: this.mixinViewModuleOptions.getDataListURL }).then(res => {
      //     if (res.code !== 0) {
      //       return
      //     }
      //     this.dataList = res.data
      //   })
      this.visible = true
    },
    // 移除
    Remove (data) {
      // data.fingerprintDeviceConfigure = 1
      this.$http.put('/tb/device/updateFinDev?id=' + data.id).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.getDataList()
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>

<style lang="scss" scoped>
</style>
